package pers.chao.tool.infrastructure.resource;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * Description: 文件操作
 *
 * @author W.Chao
 * @date 2020/11/15 11:06
 **/
public final class FilesUtil {
	private static final Logger logger = LoggerFactory.getLogger(FilesUtil.class);

	public static final boolean isOverride = true;
	public static final boolean isAppend = true;

	private FilesUtil() {
	}

	public static BufferedInputStream getInputStream(File file) {
		return IoUtil.toBuffered(IoUtil.toStream(file));
	}

	public static BufferedInputStream getInputStream(String path) {
		return getInputStream(FileUtil.file(path));
	}

	public static PrintWriter getPrintWriter(File file, boolean isAppend) {
		return FileUtil.getPrintWriter(file, StandardCharsets.UTF_8, isAppend);
	}

	public static PrintWriter getPrintWriter(String fullPath, boolean isAppend) {
		return FileUtil.getPrintWriter(FileUtil.file(fullPath), StandardCharsets.UTF_8, isAppend);
	}

	public static String getExt(File file) {
		return FileNameUtil.getSuffix(file);
	}

	public static String getFilename(File file) {
		return FileUtil.getName(file);
	}

	public static void writeString(String content, String path) {
		FileUtil.writeUtf8String(content, path);
	}

	public static void writeString(String content, File file) {
		FileUtil.writeUtf8String(content, file);
	}

	public static void appendString(String content, String path) {
		FileUtil.appendUtf8String(content, path);
	}

	public static void appendString(String content, File file) {
		FileUtil.appendUtf8String(content, file);
	}

	public static void appendFile(String src, String dest) {
		FileUtil.copy(src, dest, isAppend);
	}

	public static void appendFile(File src, File dest) {
		FileUtil.copy(src, dest, isAppend);
	}

	public static void overrideFile(String src, String dest) {
		FileUtil.copy(src, dest, isOverride);
	}

	public static void overrideFile(File src, File dest) {
		FileUtil.copy(src, dest, isOverride);
	}

	public static void createFile(String fullPath) {
		FileUtil.touch(fullPath);
	}

	public static void createFile(File path) {
		FileUtil.touch(path);
	}

	public static void createFile(String parentDir, String filename) {
		FileUtil.touch(parentDir, filename);
	}

	public static void createDir(String fullPath) {
		FileUtil.mkdir(fullPath);
	}

	public static void createDir(File path) {
		FileUtil.mkdir(path);
	}

	public static void createDir(String parentDir, String dirName) {
		FileUtil.mkdir(parentDir + File.separatorChar + dirName);
	}

	public static void del(String fullPath) {
		FileUtil.del(fullPath);
	}

	public static void del(File file) {
		FileUtil.del(file);
	}

	public static boolean isEmptyDir(String fullPath) {
		return FileUtil.isDirEmpty(FileUtil.file(fullPath));
	}

	public static boolean isEmpty(String fullPath) {
		return FileUtil.isEmpty(FileUtil.file(fullPath));
	}

	public static boolean isEmptyDir(File file) {
		return FileUtil.isDirEmpty(file);
	}

	public static boolean isEmpty(File file) {
		return FileUtil.isEmpty(file);
	}

	public static boolean isDir(String fullPath) {
		return FileUtil.isDirectory(fullPath);
	}

	public static boolean isDir(File file) {
		return FileUtil.isDirectory(file);
	}

	public static boolean isFile(String fullPath) {
		return FileUtil.isFile(fullPath);
	}

	public static boolean isFile(File file) {
		return FileUtil.isFile(file);
	}

}
